package com.zhss.designpattern.builder;

/**
 * @author fanwei
 * @desc 构造者模式现在一般都采用这个
 */
public class OptimizedBuilderPatternDemo {

    interface Builder{
        Builder field1(String val1);
        Builder field2(String val2);
        Builder field3(String val3);
        Product create();
    }

    static class ConcreteBuilder implements Builder{
        private Product product = new Product();

        public Builder field1(String val1) {
            product.setVal1(val1);
            System.out.println("val1 is :"+val1);
            return this;
        }

        public Builder field2(String val2) {
            product.setVal2(val2);
            System.out.println("val2 is :"+val2);
            return this;
        }

        public Builder field3(String val3) {
            product.setVal3(val3);
            System.out.println("val3 is :"+ val3);
            return this;
        }

        public Product create() {
            return product;
        }
    }

    static class Product{
        private String val1;
        private String val2;
        private String val3;

        public String getVal1() {
            return val1;
        }

        public void setVal1(String val1) {
            this.val1 = val1;
        }

        public String getVal2() {
            return val2;
        }

        public void setVal2(String val2) {
            this.val2 = val2;
        }

        public String getVal3() {
            return val3;
        }

        public void setVal3(String val3) {
            this.val3 = val3;
        }

        @Override
        public String toString() {
            return "Product{" +
                    "val1='" + val1 + '\'' +
                    ", val2='" + val2 + '\'' +
                    ", val3='" + val3 + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {

        Product product = new ConcreteBuilder()
                .field1("aa")
                .field2("bb")
                .field3("cc")
                .create();
        System.out.println(product);
    }
}
